package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocBackResponsibleAddDTO;
import org.jsola.hr.dto.EmpSocBackResponsibleUpdateDTO;
import org.jsola.hr.dto.SetSocBackResponsibleDTO;
import org.jsola.hr.query.EmpSocBackResponsibleQuery;
import org.jsola.hr.service.IEmpSocBackResponsibleService;
import org.jsola.hr.vo.EmpSocBackResponsibleListVO;
import org.jsola.hr.vo.EmpSocBackResponsibleVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 社保回款负责人信息
 *
 * @author zhr
 */
@RestController("hrEmpSocBackResponsibleController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocBackResponsibles")
public class EmpSocBackResponsibleController implements IEmpSocBackResponsibleControllerAPI {

    @Autowired
    private IEmpSocBackResponsibleService empSocBackResponsibleService;

    @Override
    @PostMapping
    public Result<EmpSocBackResponsibleVO> save(@Valid @RequestBody EmpSocBackResponsibleAddDTO empSocBackResponsibleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保回款负责人信息权限校验

        return Result.success(
                empSocBackResponsibleService.save(empSocBackResponsibleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocBackResponsibleUpdateDTO empSocBackResponsibleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保回款负责人信息权限校验

        int count = empSocBackResponsibleService.update(empSocBackResponsibleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{empSocBackResponsibleId}")
    public Result<Integer> delete(@PathVariable String empSocBackResponsibleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保回款负责人信息权限校验

        int count = empSocBackResponsibleService.deleteByIds(tokenUser, empSocBackResponsibleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocBackResponsibleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保回款负责人信息权限校验

        int count = empSocBackResponsibleService.deleteByIds(tokenUser, empSocBackResponsibleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocBackResponsibleId}")
    public Result<EmpSocBackResponsibleVO> findById(@PathVariable String empSocBackResponsibleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保回款负责人信息权限校验

        return Result.success(
                empSocBackResponsibleService.selectById(empSocBackResponsibleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocBackResponsibleListVO>> listAll(@Valid EmpSocBackResponsibleQuery empSocBackResponsibleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保回款负责人信息权限校验

        return Result.success(
                empSocBackResponsibleService.select(empSocBackResponsibleQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocBackResponsibleListVO>> page(@Valid EmpSocBackResponsibleQuery empSocBackResponsibleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empSocBackResponsibleService.selectPage(empSocBackResponsibleQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/settingsResponsible")
    public Result<String> settingsResponsible(@Valid @RequestBody SetSocBackResponsibleDTO settingsDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        empSocBackResponsibleService.settingsResponsible(settingsDTO, tokenUser);
        return Result.success();
    }

}

